<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
class Homestay extends Base{
    public function homestay_list(){
        $keyword=input('get.keyword');
        $state=input('get.state');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['time', '>', $fromtime],
            ['time', '<', $totime]
        ];
        if($state==0){
            array_push($map,['state','<',3]);
        }else{
            array_push($map,['state','=',$state]);
        }
        $list=Db::name('homestay')->where($map)->where('des','like',"%{$keyword}%")->order('id desc')->paginate(10);
        $items = $list->items();
        foreach ($items as $key => $value) {
            $items[$key]['img']=explode(',', $items[$key]['imgs'])[0];
        }
        $counts=Db::name('homestay')->where($map)->where('des','like',"%{$keyword}%")->count();
        $data=$list->all();
        $this->assign('keyword',$keyword);
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('state',$state);
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('items',$items);
        $this->assign('counts',$counts);
        return $this->fetch();
    }
    public function homestay_add(){
        if(request()->isGet()){
            $this->assign('domain',$this->request->domain());
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['des']=$param['des'];
            $data['bed']=$param['bed'];
            $data['storey']=$param['storey'];
            $data['area']=$param['area'];
            $data['breakfast']=$param['breakfast'];
            $data['peoples']=$param['peoples'];
            $data['wifi']=$param['wifi'];
            $data['more_bed']=$param['more_bed'];
            $data['non_smoking']=$param['non_smoking'];
            $data['imgs']=$param['imgs'];
            $data['price']=$param['price'];
            $data['state']=$param['state'];
            $data['sort']=$param['sort'];
            $data['time']=time();
            $data['uptime']=time();
            $res=Db::name('homestay')->insert($data);
            if($res){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function homestay_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('homestay')->where('id',$param['id'])->find();
            if($data['imgs']){
                $data['imgsarr']=explode(",",$data['imgs']);
            }else{
                $data['imgsarr']=array();
            }
            $this->assign('data',$data);
            $this->assign('domain',$this->request->domain());
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['des']=$param['des'];
            $data['bed']=$param['bed'];
            $data['storey']=$param['storey'];
            $data['area']=$param['area'];
            $data['breakfast']=$param['breakfast'];
            $data['peoples']=$param['peoples'];
            $data['wifi']=$param['wifi'];
            $data['more_bed']=$param['more_bed'];
            $data['non_smoking']=$param['non_smoking'];
            $data['imgs']=$param['imgs'];
            $data['price']=$param['price'];
            $data['state']=$param['state'];
            $data['sort']=$param['sort'];
            $data['uptime']=time();
            $res= Db::name('homestay')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function homestay_del(){
        $param = input('post.');
        $res= Db::name('homestay')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('homestay')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('homestay')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
}
